package com.wodsy.phoenix.datadao.userc.dao.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.wodsy.phoenix.datadao.base.BaseHibernateDao;
import com.wodsy.phoenix.datadao.userc.dao.UserManagementDao;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.userc.model.UserBasic;





@Repository("userManagementDao")
public class UserManagementDaoImpl extends BaseHibernateDao<UserBasic,Integer> implements UserManagementDao {

	@Override
	public Class<UserBasic> getModelClass() {
		return UserBasic.class;
	}

	/**
	 * 通过邮箱 查找用户
	 * @param email 邮箱
	 * @return 用户的完整信息
	 */
	public UserBasic findByEmail(String email) {
		
		try {
			Criteria criteria = getSession().createCriteria(getModelClass());
			criteria.add(Restrictions.eq("userEmail", email));
			return (UserBasic) criteria.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 通过手机号查找用户
	 * @param phone
	 * @return
	 */
	public UserBasic findByPhone(long phone) {
		try {
			Criteria criteria = getSession().createCriteria(getModelClass());
			criteria.add(Restrictions.eq("userTel", phone));
			return (UserBasic) criteria.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 通过用户账号查找用户
	 * @param account
	 * @return
	 */
	public UserBasic findByAccount(String account){
		try {
			Criteria criteria = getSession().createCriteria(getModelClass());
			criteria.add(Restrictions.eq("userAccount", account));
			return (UserBasic) criteria.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Pager<UserBasic> getUsersByParam(Pager<UserBasic> pager,
			Map<String, Object> param) {
		String username = (String) param.get("username");
		String role = (String)param.get("role");
		Criteria criteria = getSession().createCriteria(getModelClass());
		
		
		if(StringUtils.isNotBlank(username)){
			criteria.add(Restrictions.like("username", "%"+username+"%"));
		}
		if(StringUtils.isNotBlank(role)){
			criteria.add(Restrictions.eq("role", role));
		}
		criteria.addOrder(Property.forName("rectime").desc());
		return toPager(pager, criteria);
	}
	

	public UserBasic getUserByEmail(String email) {
		Criteria criteria = getSession().createCriteria(getModelClass());
		criteria.add(Restrictions.eq("username", email));
		return (UserBasic) criteria.uniqueResult();
	}

	@Override
    @Autowired
    @Qualifier(value="userc")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}
